<template>
  <div class="yun-view">
    <div class="yun-login">
      <div class="yun-title">欢迎登录</div>
      <el-form ref="yFormDom" :model="yForm" :rules="rules" size="large">
        <el-form-item prop="userName">
          <el-input type="text" v-model="yForm.userName" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="yForm.password"  placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" class="login-btn" @click="loginEvent">登录 / 注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import {ref,reactive} from 'vue'
import {useRouter} from 'vue-router'
import {login} from '@/api/index.js'
import {useUserStore} from '@/store/userStore'
const store = useUserStore()
const router = useRouter()
const yForm = reactive({
  userName:"",
  password:''
})
const yFormDom = ref(null)
const rules = reactive({
  userName:[
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  password:[
    {required: true, message: '请输入密码', trigger: 'blur'}
  ]
})
const loginEvent = ()=>{
  console.log(yForm)
  yFormDom.value.validate(async (valid, fields) => {
    console.log(valid, fields)
    if (valid) {
      let params = {
        username:yForm.userName,
        password:yForm.password
      }
      console.log(yForm)
      let res = await login(params)
      console.log(res)
      if(res.code==200){
        localStorage.setItem('auth-token',res.data.token)
        localStorage.setItem('userInfo',res.data.userinfo)
        store.setUserInfo(res.data.userinfo)
        router.push('/home')
      }  
    }
  })
}
</script>
<style lang="scss" scoped>
.yun-view{
  background: var(--main-color);
  display: flex;
  justify-content: center;
  align-items: center;
  .yun-login{
    width:400px;
    background: #fff;
    border-radius: 8px;
    padding:20px;
    .yun-title{
      font-size:22px;
      text-align: center;
      line-height: 40px;
      margin-bottom:20px;
      font-weight: bold;
    }
    .login-btn{
      width:100%;
      letter-spacing: 2px;
    }
  }
}
</style>